package com.tebiecloud.goods.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.goods.client.model.entity.GoodsProcess;
import com.tebiecloud.goods.server.service.GoodsProcessService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 *  前端控制器
 * @author tebie
 * @date 2019-10-28
 */
@RestController
@RequestMapping("/goodsprocess")
public class GoodsProcessController {

    @Autowired
    private GoodsProcessService goodsProcessService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<GoodsProcess>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        GoodsProcess query = pageParams.mapToObject(GoodsProcess.class);
        QueryWrapper<GoodsProcess> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(goodsProcessService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<GoodsProcess> get(@RequestParam("id") Long id) {
        GoodsProcess entity = goodsProcessService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param goodsProcessId 主键,
     * @param goodsId 商品id,
     * @param serviceEffect 服务功效,
     * @param servicePart 服务部位,
     * @param serviceProcess 服务流程,
     * @param deleted 删除状态：0没有删除1已被删除
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "goodsProcessId", required = false) Long goodsProcessId,
        @RequestParam(value = "goodsId", required = false) Long goodsId,
        @RequestParam(value = "serviceEffect", required = false) String serviceEffect,
        @RequestParam(value = "servicePart", required = false) String servicePart,
        @RequestParam(value = "serviceProcess", required = false) String serviceProcess,
        @RequestParam(value = "deleted", required = false) Integer deleted
    ) {
        GoodsProcess entity = new GoodsProcess();
        entity.setGoodsProcessId(goodsProcessId);
        entity.setGoodsId(goodsId);
        entity.setServiceEffect(serviceEffect);
        entity.setServicePart(servicePart);
        entity.setServiceProcess(serviceProcess);
        entity.setDeleted(deleted);
        goodsProcessService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param goodsProcessId 主键
     * @param goodsId 商品id
     * @param serviceEffect 服务功效
     * @param servicePart 服务部位
     * @param serviceProcess 服务流程
     * @param deleted 删除状态：0没有删除1已被删除
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "goodsProcessId", required = false) Long goodsProcessId,
            @RequestParam(value = "goodsId", required = false) Long goodsId,
            @RequestParam(value = "serviceEffect", required = false) String serviceEffect,
            @RequestParam(value = "servicePart", required = false) String servicePart,
            @RequestParam(value = "serviceProcess", required = false) String serviceProcess,
            @RequestParam(value = "deleted", required = false) Integer deleted
    ) {
        GoodsProcess entity = new GoodsProcess();
        entity.setGoodsProcessId(goodsProcessId);
        entity.setGoodsId(goodsId);
        entity.setServiceEffect(serviceEffect);
        entity.setServicePart(servicePart);
        entity.setServiceProcess(serviceProcess);
        entity.setDeleted(deleted);
        goodsProcessService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            goodsProcessService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            goodsProcessService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
